<?php
/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Story
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Story_Plugin_Menus
{

  public function canCreateStories()
  {
    // Must be logged in
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }

    // Must be able to create stories
    if( !Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'create') ) {
      return false;
    }

    return true;
  }

  public function canViewStories()
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    
    // Must be able to view stories
    if( !Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'view') ) {
      return false;
    }

    return true;
  }  
  
  
  public function onMenuInitialize_StoryGutterList($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $story = Engine_Api::_()->core()->getSubject('story');

    if( !($story instanceof Story_Model_Story) ) {
      return false;
    } 
    
    // Modify params
    $params = $row->params;
    $params['params']['user'] = $story->owner_id;
    return $params;
  }

  public function onMenuInitialize_StoryGutterCreate($row)
  {
    // Must be logged in
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }

    // Must be able to create stories
    if( !Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'create') ) {
      return false;
    }    
    
    return true;
  }
  
  public function onMenuInitialize_StoryGutterChapter($row)
  {
  	// Must be logged in
  	$viewer = Engine_Api::_()->user()->getViewer();
  	if( !$viewer || !$viewer->getIdentity() ) {
  		return false;
  	}
  
  	// Must be able to create stories
  	if( !Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'create') ) {
  		return false;
  	}
  
  	if( !Engine_Api::_()->core()->hasSubject() ) {
  		return false;
  	}
  	
  	$story = Engine_Api::_()->core()->getSubject('story');
  	
  	if( !($story instanceof Story_Model_Story) ) {
  		return false;
  	}
  	 
  	if($story->allowChapters() && $story->isOwner($viewer)){
  		
  		if($story->canView(Story_Model_Category::STORY_THREAD) && $story->invite_request == 1) {
  			return false;
  		}
  		  
  		$chapter_table = Engine_Api::_()->getItemTable("story_chapter");
  		$last_chapter = $chapter_table->getLastChapter($story->getIdentity());
  		$params = $row->params;
  		if($last_chapter){
  			// Modify params
  			$params['params']['chapter_id'] = $last_chapter->getIdentity();
  			$params['params']['story_id'] = $story->getIdentity();
  		}
  		return $params;
  	}
  	 
  	
  	
  	return true;
  }
  
  public function onMenuInitialize_StoryGutterInvite($row)
  {
  	// Must be logged in
  	$viewer = Engine_Api::_()->user()->getViewer();
  	if( !$viewer || !$viewer->getIdentity() ) {
  		return false;
  	}
  
  	// Must be able to create stories
  	if( !Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'create') ) {
  		return false;
  	}
  
  	if( !Engine_Api::_()->core()->hasSubject() ) {
  		return false;
  	}
  	 
  	$story = Engine_Api::_()->core()->getSubject('story');
  	 
  	if( !($story instanceof Story_Model_Story) ) {
  		return false;
  	}
  
  	//need to check if the owner ca
  	
  	if($story->canView(Story_Model_Category::STORY_THREAD) && $story->isOwner($viewer)){
  			
  		if($story->invite_request == 1) return false;
  		
  		$chapter_table = Engine_Api::_()->getItemTable("story_chapter");
  		$last_chapter = $chapter_table->getLastChapter($story->getIdentity());
  		$params = $row->params;
  		if($last_chapter){
  			// Modify params
  			$params['params']['chapter_id'] = $last_chapter->getIdentity();
  			$params['params']['story_id'] = $story->getIdentity();
  			$params['params']['type'] = "invite";
  		}
  		return $params;
  	}
  
  	 
  	 
  	return true;
  }

  public function onMenuInitialize_StoryGutterEdit($row)
  {
  	
  	return false;
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $story = Engine_Api::_()->core()->getSubject('story');

    if( !($story instanceof Story_Model_Story) ) {
      return false;
    }    
    
    if( !$story->authorization()->isAllowed($viewer, 'edit') ) {
      return false;
    }

    // Modify params
    $params = $row->params;
    $params['params']['story_id'] = $story->getIdentity();
    return $params;
  }

  public function onMenuInitialize_StoryGutterDelete($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $story = Engine_Api::_()->core()->getSubject('story');

    if( !($story instanceof Story_Model_Story) ) {
      return false;
    }
    
    if( !$story->authorization()->isAllowed($viewer, 'delete') ) {
      return false;
    }

    // Modify params
    $params = $row->params;
    $params['params']['story_id'] = $story->getIdentity();
    return $params;
  }
  
  public function onMenuInitialize_StoryGutterDelChapter($row)
  {
  	if( !Engine_Api::_()->core()->hasSubject() ) {
  		return false;
  	}
  
  	$viewer = Engine_Api::_()->user()->getViewer();
  	$story = Engine_Api::_()->core()->getSubject('story');
  	
  	if(!$story->isOwner($viewer)){
  		return false;	
  	}
  
  	if( !($story instanceof Story_Model_Story) ) {
  		return false;
  	}
  	
  	if(!$story->allowChapters()) {
  		return false;
  	}  	 
  
  	$request = Zend_Controller_Front::getInstance()->getRequest();
  	$chapter_id = $request->getParam("chapter_id");
  	
  	if(empty($chapter_id)){
  		$chapter = $story->getFirstChapter();
  		if(!$chapter){
  			return false;
  		}  		
  		$chapter_id = $chapter->getIdentity(); 
  	}
  	
  	
  	// Modify params
  	$params = $row->params;
  	$params['params']['story_id'] = $story->getIdentity();
  	$params['params']['chapter_id'] = $chapter_id;
  	return $params;
  }
  
  
  public function onMenuInitialize_StoryPhotosList($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $story = Engine_Api::_()->core()->getSubject('story');

    if( !($story instanceof Story_Model_Story) ) {
      return false;
    }
    
    if( !$story->authorization()->isAllowed($viewer, 'view') ) {
      return false;
    }
    
    // Modify params
    $params = $row->params;
    $params['params']['subject'] = $story->getGuid();
    return $params;
  }
  
  
  public function onMenuInitialize_StoryPhotosManage($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $story = Engine_Api::_()->core()->getSubject('story');

    if( !($story instanceof Story_Model_Story) ) {
      return false;
    }
    
    if( !$story->authorization()->isAllowed($viewer, 'photo') ) {
      return false;
    }
    
    // Modify params
    $params = $row->params;
    $params['params']['subject'] = $story->getGuid();
    return $params;
  }
  
  
  public function onMenuInitialize_StoryPhotosUpload($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $story = Engine_Api::_()->core()->getSubject('story');

    if( !($story instanceof Story_Model_Story) ) {
      return false;
    }
    
    if( !$story->authorization()->isAllowed($viewer, 'photo') ) {
      return false;
    }
    
    // Modify params
    $params = $row->params;
    $params['params']['subject'] = $story->getGuid();
    return $params;
  }  
  
}